<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Grades extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->page_auth->secure_page('teacher');
		$this->load->library(array('grocery_CRUD','upload','calendar'));
		$this->load->helper('form');
		$this->load->model(array('M_student_grades', 'M_student_subjects', 'M_subjects', 'M_teachers','M_gperiods','M_student'));
	}
	
	public function index()
	{
		show_404();
	}
	
	public function subject_grades($subj_id = '')
	{		
		if($subj_id != '' AND is_numeric($subj_id))
		{
			$this->view_data['subj_data'] = $this->M_subjects->get_subject($subj_id);
			$this->view_data['teachers_schedule'] = $this->M_teachers->get_my_schedules();
			
			$students = $this->M_student_grades->get_students_per_subject($subj_id);
			$s_grades = $this->M_student_grades->get_current_entries($subj_id);
			
			$this->view_data['periods'] = $this->current_grading_period;
			$this->view_data['students'] = $students;
			$this->view_data['grades_data'] = $s_grades;
			$this->view_data['id'] = $subj_id;
			
			if($_POST)
			{
				$grades = $this->input->post("grades");
				var_dump($grades);
				foreach($grades AS $g => $val)
				{
					$l_grade = '';
					if($val[$this->current_grading_period] >= 90)
					{
						$l_grade = 'A';
					}elseif($val[$this->current_grading_period] >= 85)
					{
						$l_grade = 'P';
					}elseif($val[$this->current_grading_period] >= 80)
					{
						$l_grade = 'AP';
					}elseif($val[$this->current_grading_period] >= 75)
					{
						$l_grade = 'D';
					}else
					{
						$l_grade = 'B';
					}
					$data = array(
								'grade' => $val[$this->current_grading_period],
								'grading_period' => $this->current_grading_period,
								'enrollment_id' => $val['e_id'],
								'subject_id' => $subj_id,
								'letter_grade' => $l_grade
							);
					
					if(!($this->M_student_grades->update_per_grading_period($data)))
					{
						break;
					}
				}
				redirect('grades/subject_grades/' . $subj_id);
			}
		}else
		{
			$this->view_data['system_message'] = $this->build_error('error', 'Invalid subject ID');
		}
	}
	
	public function new_grades($subj_id = '')
	{
		if($subj_id != '' AND is_numeric($subj_id))
		{
			$subj_data = $this->M_subjects->get_subject($subj_id);
			$stud_data = $this->M_student_grades->get_students_per_subject($subj_id);
			$current_entries = $this->M_student_grades->get_current_entries($subj_id);
			
			var_dump($stud_data);
			
			$this->view_data['entries'] = $current_entries;
			$this->view_data['subj_data'] = $subj_data;
			$this->view_data['student_data'] = $stud_data;
			
			
			
			if($this->input->post("grades"))
			{
				$l_grades = $this->input->post("l_grades");
				$grades = $this->input->post("grades");
				$id_arr = $this->input->post("id_arr");
				for($i = 0; $i < count($id_arr); $i++)
				{
					$id = $id_arr[$i];
					for($x = 1; $x <= 4; $x++)
					{
						$grade_details = array(
											'grade' => $grades[$id][$x],
											'grading_period' => $x,
											'enrollment_id' => $id,
											'letter_grade' => $l_grades[$id][$x],
											'subject_id' => $subj_id);
						$flag = $this->M_student_grades->insert_grade_per_grading_period($grade_details);
						if($flag == FALSE)
						{
							$this->view_data['system_message'] = $this->build_error('error', 'There was a problem inserting grades');
						}
					}
				}
				redirect('/grades/subject_grades/' . $subj_id, 'location');
			}
			$this->view_data['teachers_schedule'] = $this->M_teachers->get_my_schedules();
		}else
		{
			$this->view_data['system_message'] = $this->build_error('error', 'Invalid subject ID');
		}
	}
	
	public function _get_letter_grade()
	{
	}
}

?>